<template>
  <div class="h-full main-container">
    <el-tabs class="h-full" v-model="tabs.current" @tab-change="tabChange">
      <el-tab-pane v-for="item in tabs.lists" :label="item.caption" :name="item.name">
        <div class="h-full" v-if="tabs.current === item.name">
          <router-view />
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<style lang="scss" scoped>
.main-container {
  width: 100%;
  border-left: solid 1px var(--ep-border-color);
  padding: 0 8px 8px 8px;

  :deep(.ep-tabs__header) {
    margin-bottom: 8px;
  }

  :deep(.ep-tabs__content) {
    height: calc(100% - 56px)
  }

  :deep(.ep-tab-pane) {
    height: 100%;
  }

  :deep(.ep-tabs__item) {
    padding: 0 8px;
  }
}
</style>

<script lang="ts" setup>
import { reactive } from 'vue'
import router from '~/router'
import { tabStore } from '~/store';
import { MenuItem } from '~/types/ui/MenuItem';

const currentRoute = router.currentRoute.value;
const tabStoreInstance = tabStore();
const currentItem = tabStoreInstance.contains(currentRoute.name as string, currentRoute.meta.caption as string, currentRoute.path);

var tabs = reactive<{
  current: string,
  lists: MenuItem[]
}>({
  current: currentItem.name,
  lists: tabStoreInstance.tabs
});

router.afterEach((to) => {
  const store = tabStore();
  const current = store.contains(to.name as string, to.meta.caption as string, to.path as string);
  tabs.current = current.name;
})

const tabChange = (name: string) => {
  tabs.current = name
  const store = tabStore()
  const current = store.contains(name, null, null)
  router.push(current.path)
}
</script>
